package com.ym.manager.admin.banner;

import com.ym.manager.admin.banner.model.BannerInfo;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.Pages;
import com.ym.utils.ResultUtil;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/banner/")
public class BannerController {

    @Autowired
    private BannerModel bannerModel;

    /**
     * 页面：banner列表
     * @return
     */
    @RequestMapping("list")
    public String list(){
        return "/banner/list";
    }

    /**
     * 功能：banner列表
     * @return
     */
    @RequestMapping("listData")
    @ResponseBody
    public String listData(@RequestParam(defaultValue = "0") int page,
                           @RequestParam(defaultValue = "10") int limit,
                           @RequestParam(defaultValue = "1") int placeId){
        Map<String,Object> map = ResultUtil.result();
        try {
            Pages<BannerInfo> pages = bannerModel.listData(page,limit,placeId);
            //查询数据
            map.put("data",pages.getList());
            map.put("count",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",-1000);
            map.put("msg","系统异常");
        }
        return ResultUtil.toJSON(map);
    }

    /**
     * 页面：新增banner
     * @return
     */
    @GetMapping("add")
    public String add(){
        return "/banner/add";
    }

    /**
     * 功能：新增banner
     * @param bannerInfo
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public String add(BannerInfo bannerInfo,@CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            bannerInfo.setCreateUser(user.getName());
            bannerInfo.setUrl(bannerInfo.getBgUrl());
            int i = bannerModel.insert(bannerInfo);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?"新增成功":"新增失败");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：删除banner
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public String delete(@RequestParam(defaultValue = "0") int id){
        Map<String,Object> result = ResultUtil.result();
        if(id<=0){
            result.put("code",-1000);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = bannerModel.delete(id);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?"删除成功":"删除失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }
}
